﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zzw
{
    public static class StringHelper
    {
        #region Is
        #region IsNull
        public static bool IsNull(this string value)
        {
            return value == null;
        }
        public static bool IsNoNull(this string value)
        {
            return !value.IsNull();
        }
        public static bool IsNullOrEmpty(this string value)
        {
            return value == null || value == string.Empty;
        }
        public static bool IsNoNullOrEmpty(this string value)
        {
            return !value.IsNullOrEmpty();
        }
        public static bool IsNullOrEmptyOrSpace(this string value)
        {
            return value == null || value.Trim() == string.Empty;
        }
        public static bool IsNoNullOrEmptyOrSpace(this string value)
        {
            return !value.IsNullOrEmptyOrSpace();
        }
        #endregion
        #region IsNumber
        public static bool IsInt(this string value)
        {
            try
            {
                int i = int.Parse(value);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool IsNoInt(this string value)
        {
            return !IsInt(value);
        }
        public static bool IsInt(this char value)
        {
            int ascii = (int)value;
            if (ascii >= 48 && ascii <= 57)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool IsNoInt(this char value)
        {
            return !IsInt(value);
        }
        public static bool IsIntChinese(this string value)
        {
            try
            {
                int i = value.ToIntChinese();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion
        #region IsUrl
        public static bool IsUrl(this string value)
        {
            try
            {
                Uri u = new Uri(value);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion
        #region IsIp
        /// <summary>
        /// 判断是否为正确的IP地址，IP范围（0.0.0.0～255.255.255）
        /// </summary>
        /// <param name="ip">需验证的IP地址</param>
        /// <returns></returns>
        public static bool IsIP(this string ip)
        {
            return System.Text.RegularExpressions.Regex.Match(ip, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$").Success;
        }
        #endregion
        #endregion
        #region To
        #region ToInt
        public static int ToInt(this string value, int defValue)
        {
            try
            {
                return int.Parse(value);
            }
            catch (Exception)
            {
                return defValue;
            }
        }
        public static int ToInt(this string value)
        {
            return int.Parse(value);
        }
        public static int ToIntChinese(this string value)
        {
            int intValue = 0;
            int units = 0;
            for (int i = 0; i < value.Length; i++)
            {
                switch (value[i].ToString())
                {
                    case "〇":
                        intValue *= 10;
                        break;
                    case "一":
                        intValue = intValue * 10 + 1;
                        break;
                    case "二":
                        intValue = intValue * 10 + 2;
                        break;
                    case "三":
                        intValue = intValue * 10 + 3;
                        break;
                    case "四":
                        intValue = intValue * 10 + 4;
                        break;
                    case "五":
                        intValue = intValue * 10 + 5;
                        break;
                    case "六":
                        intValue = intValue * 10 + 6;
                        break;
                    case "七":
                        intValue = intValue * 10 + 7;
                        break;
                    case "八":
                        intValue = intValue * 10 + 8;
                        break;
                    case "九":
                        intValue = intValue * 10 + 9;
                        break;
                    case "零":
                        if(units == 0)//五零零一 == 5001
                        {
                            intValue *= 10;
                            break;
                        }
                        else //五千零一 == 5001
                        {
                            while (intValue < units)
                            {
                                intValue *= 10;
                            }
                            intValue += ToIntChinese(value.Substring(i + 1));
                            return intValue;
                        }
                    case "十":
                        units = units >= 10 ? units : 10;
                        if (intValue == 0)
                        {
                            intValue = 1;
                        }
                        break;
                    case "百":
                        units = units >= 100 ? units : 100;
                        if (intValue == 0)
                        {
                            intValue = 1;
                        }
                        break;
                    case "千":
                        units = units >= 1000 ? units : 1000;
                        if (intValue == 0)
                        {
                            intValue = 1;
                        }
                        break;
                    case "万":
                        units = units >= 10000 ? units : 10000;
                        if (intValue == 0)
                        {
                            intValue = 1;
                        }
                        break;
                    default:
                        break;
                }
            }
            while (intValue < units)
            {
                intValue *= 10;
            }
            return intValue;
        }

        public static int ToIntChinese(this string value,int defValue)
        {
            try
            {
                return ToIntChinese(value);
            }
            catch (Exception)
            {
                return defValue;
            }
        }
        #endregion

        #endregion
        #region Trim
        #endregion
        #region 判断字节编码类型
        /// <summary>
        /// 获取当前字节流的编码类型
        /// </summary>
        /// <param name="srcBytes"></param>
        /// <returns></returns>
        public static Encoding GetEncoding(this byte[] srcBytes)
        {
            byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
            byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
            byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM 
            Encoding reVal = Encoding.Default;

            if (IsUTF8Bytes(srcBytes) || (srcBytes[0] == 0xEF && srcBytes[1] == 0xBB && srcBytes[2] == 0xBF))
            {
                reVal = Encoding.UTF8;
            }
            else if (srcBytes[0] == 0xFE && srcBytes[1] == 0xFF && srcBytes[2] == 0x00)
            {
                reVal = Encoding.BigEndianUnicode;
            }
            else if (srcBytes[0] == 0xFF && srcBytes[1] == 0xFE && srcBytes[2] == 0x41)
            {
                reVal = Encoding.Unicode;
            }
            return reVal;
        }
        /// <summary> 
        /// 判断是否是不带 BOM 的 UTF8 格式 
        /// </summary> 
        /// <param name=“data“></param> 
        /// <returns></returns> 
        private static bool IsUTF8Bytes(byte[] data)
        {
            int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 
            byte curByte; //当前分析的字节. 
            for (int i = 0; i < data.Length; i++)
            {
                curByte = data[i];
                if (charByteCounter == 1)
                {
                    if (curByte >= 0x80)
                    {
                        //判断当前 
                        while (((curByte <<= 1) & 0x80) != 0)
                        {
                            charByteCounter++;
                        }
                        //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
                        if (charByteCounter == 1 || charByteCounter > 6)
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    //若是UTF-8 此时第一位必须为1 
                    if ((curByte & 0xC0) != 0x80)
                    {
                        return false;
                    }
                    charByteCounter--;
                }
            }
            if (charByteCounter > 1)
            {
                throw new Exception("非预期的byte格式");
            }
            return true;
        }
        #endregion

    }
}
